import { Injectable } from '@angular/core';
import { invoke } from '@tauri-apps/api/core';
import { path } from '@tauri-apps/api';
import { g } from './global-data';


@Injectable({
    providedIn: 'root'
})
export class TauriService {

    async open_source_homepage() {
        invoke('open_source_homepage');
    }

    async http_get(url: string) {
        return await invoke('http_get', {url});
    }

    async get_tianditu_route(url: string) {
        return await invoke('get_tianditu_route', {url});
    }

    async save_map_screenshot(file_path: string, img_url: string) {
        invoke('save_map_screenshot', {
            filePath: file_path,
            imgUrl: img_url,
        });
    }

    async get_current_path() {
        const home_dir = await path.homeDir();
        return await invoke('read_text_file', {
            filePath: await path.join(home_dir, '.spidermap', 'current_path.cfg'),
        }) || home_dir;
    }

    async save_current_path(current_path: string) {
        invoke('save_current_path', {
            currentPath: current_path,
        });
    }

    async load_map_config() {
        const map_config: any = await invoke('load_map_config');
        return !!map_config ? JSON.parse(map_config) : null;
    }

    async load_user_data() {
        const user_data: any = await invoke('load_user_data');
        return !!user_data ? JSON.parse(user_data) : null;
    }

    async load_visible_tracks() {
        return JSON.parse(await invoke('load_visible_tracks'));
    }

    async load_track_data(track_uuid: string) {
        let track_line_points = g.track_line_points_cache.get(track_uuid);

        if (!!track_line_points) {
            return track_line_points;
        }

        track_line_points = JSON.parse(
            await invoke('load_track_data', {
                trackUuid: track_uuid,
            })
        );

        if (track_line_points.length < 1) {
            return null;
        }

        g.track_line_points_cache.set(track_uuid, track_line_points);
        return track_line_points;
    }

    async save_track_data(track_uuid: string) {
        invoke('save_track_data', {
            trackUuid: track_uuid,
            trackData: JSON.stringify(g.track_line_points_cache.get(track_uuid)),
        })
    }

    async delete_track_data(track_uuid: string) {
        invoke('delete_track_data', {
            trackUuid: track_uuid,
        })
    }

    async load_track_files(file_paths: string[]) {
        return await invoke('load_track_files', {
            filePaths: file_paths,
        });
    }

    async save_track_file(file_path: string, file_content: string) {
        invoke('save_track_file', {
            filePath: file_path,
            fileContent: file_content,
        })
    }

    async save_user_data() {
        const user_data = structuredClone(g.user_data_tree[0]);
        this.delete_unused_data(user_data);

        invoke('save_user_data', { data: JSON.stringify(user_data) })
    }

    async save_snapshot_and_exit() {
        const user_data = structuredClone(g.user_data_tree[0]);
        this.delete_unused_data(user_data);

        const map_config = {
            zoom: g.map_canvas.zoom,
            centre_fpx: g.map_canvas.centre_fpx,
            centre_fpy: g.map_canvas.centre_fpy,
            map_source_list: g.map_source_list,
        }

        const visible_track_uuids = []
        for (const track_uuid of g.visible_tracks.keys()) {
            visible_track_uuids.push(track_uuid);
        }

        invoke('save_snapshot_and_exit', {
            userData: JSON.stringify(user_data),
            mapConfig: JSON.stringify(map_config),
            visibleTrackUuids: JSON.stringify(visible_track_uuids),
        })
    }

    async open_photo_file(file_path: string) {
        return await invoke('open_photo_file', {
            filePath: file_path,
        });
    }

    delete_unused_data(node: any) {
        delete node.parent;
        delete node.style;
        if (!!node.children) {
            for (const child of node.children) {
                this.delete_unused_data(child);
            }
        }
    }

}
